var cardArray = new Array();
var clickCardArray = new Array();
for(i = 0; ; i++){
  if ((element = clickCard(i)) == null) {
    break;
  }
  cardArray[i] = element.style.backgroundColor;
  clickCardArray[i] = 0;
}
for(j = 0; j < cardArray.length - 1; j++) {
  if(clickCardArray[j] == 0) {
    clickCard(j);
    clickCardArray[j] = 1;
    for(k = j + 1; k < cardArray.length; k++) {
      if(clickCardArray[k] == 0) {
        if(cardArray[j] == cardArray[k]) {
          clickCard(k);
          clickCardArray[k] = 1;
          break;
        }
      }
    }
  }
}

function clickCard(cardnum){
  if((element = document.getElementById("card" + cardnum)) == null) {
    return null;
  }
  var myevent = document.createEvent('MouseEvents');
  myevent.initEvent('click', false, true);
  element.dispatchEvent(myevent);
  return element;
}
